package com.toe.sso.security.service.user;

import com.toe.common.exception.ToeServiceException;
import com.toe.common.utils.TimeUtil;
import com.toe.pojo.entity.user.UserUsertbl;
import com.toe.sso.mapper.UserMapper;
import com.toe.sso.pojo.domain.UserUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserSSOUserDetailsService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
        // 获取与用户名匹配的管理员信息
        UserUsertbl user = null;
        try {
             user = userMapper.findByPhone(phone);
        } catch (ToeServiceException e) {
            throw new BadCredentialsException("登录失败，用户名不正确！");
        }
        if(user==null){
            return null;
        }
        // 检查管理员是否启用
        if (user.getIsEnable() != 1) {
            throw new BadCredentialsException("登录失败，用户账号已经被禁用！");
        }
        UserUserDetails userDetails=new UserUserDetails();
        BeanUtils.copyProperties(user,userDetails);
        int age = (int) TimeUtil.getAge(user.getBirthday());
        userDetails.setAge(age);
        // 返回
        return userDetails;
    }
}
